<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.factors.continuous.ContinuousFactor &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../../index.html">
              <img class="logo" src="../../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.factors.continuous.ContinuousFactor</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">division</span>

<span class="kn">import</span> <span class="nn">types</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">scipy.integrate</span> <span class="k">as</span> <span class="nn">integrate</span>

<span class="kn">from</span> <span class="nn">pgmpy.factors.base</span> <span class="k">import</span> <span class="n">BaseFactor</span>
<span class="kn">from</span> <span class="nn">pgmpy.factors.distributions</span> <span class="k">import</span> <span class="n">GaussianDistribution</span><span class="p">,</span> <span class="n">CustomDistribution</span>


<div class="viewcode-block" id="ContinuousFactor"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor">[docs]</a><span class="k">class</span> <span class="nc">ContinuousFactor</span><span class="p">(</span><span class="n">BaseFactor</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for factors representing various multivariate</span>
<span class="sd">    representations.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">pdf</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list or array-like</span>
<span class="sd">            The variables for wich the distribution is defined.</span>

<span class="sd">        pdf: function</span>
<span class="sd">            The probability density function of the distribution.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from scipy.special import beta</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        # Two variable dirichlet distribution with alpha = (1,2)</span>
<span class="sd">        &gt;&gt;&gt; def dirichlet_pdf(x, y):</span>
<span class="sd">        ...     return (np.power(x, 1) * np.power(y, 2)) / beta(x, y)</span>
<span class="sd">        &gt;&gt;&gt; dirichlet_factor = ContinuousFactor([&#39;x&#39;, &#39;y&#39;], dirichlet_pdf)</span>
<span class="sd">        &gt;&gt;&gt; dirichlet_factor.scope()</span>
<span class="sd">        [&#39;x&#39;, &#39;y&#39;]</span>
<span class="sd">        &gt;&gt;&gt; dirichlet_factor.assignment(5,6)</span>
<span class="sd">        226800.0</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;variables: Expected type list or array-like, &quot;</span>
                            <span class="s2">&quot;got type </span><span class="si">{var_type}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var_type</span><span class="o">=</span><span class="nb">type</span><span class="p">(</span><span class="n">variables</span><span class="p">)))</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">variables</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">variables</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Variable names cannot be same.&quot;</span><span class="p">)</span>

        <span class="n">variables</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">variables</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pdf</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">pdf</span> <span class="o">==</span> <span class="s1">&#39;gaussian&#39;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">distribution</span> <span class="o">=</span> <span class="n">GaussianDistribution</span><span class="p">(</span>
                    <span class="n">variables</span><span class="o">=</span><span class="n">variables</span><span class="p">,</span>
                    <span class="n">mean</span><span class="o">=</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;mean&#39;</span><span class="p">],</span>
                    <span class="n">covariance</span><span class="o">=</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;covariance&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{dist}</span><span class="s2"> distribution not supported.&quot;</span><span class="p">,</span>
                                          <span class="s2">&quot;Please use CustomDistribution&quot;</span><span class="o">.</span>
                                          <span class="nb">format</span><span class="p">(</span><span class="n">dist</span><span class="o">=</span><span class="n">pdf</span><span class="p">))</span>

        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pdf</span><span class="p">,</span> <span class="n">CustomDistribution</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">distribution</span> <span class="o">=</span> <span class="n">pdf</span>

        <span class="k">elif</span> <span class="n">callable</span><span class="p">(</span><span class="n">pdf</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">distribution</span> <span class="o">=</span> <span class="n">CustomDistribution</span><span class="p">(</span>
                <span class="n">variables</span><span class="o">=</span><span class="n">variables</span><span class="p">,</span>
                <span class="n">distribution</span><span class="o">=</span><span class="n">pdf</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;pdf: Expected type: str or function, &quot;</span><span class="p">,</span>
                             <span class="s2">&quot;Got: </span><span class="si">{instance}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="nb">type</span><span class="p">(</span><span class="n">variables</span><span class="p">)))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">pdf</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the pdf of the ContinuousFactor.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">pdf</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">variable</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>

<div class="viewcode-block" id="ContinuousFactor.scope"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.scope">[docs]</a>    <span class="k">def</span> <span class="nf">scope</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the scope of the factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        list: List of variable names in the scope of the factor.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        &gt;&gt;&gt; from scipy.stats import multivariate_normal</span>
<span class="sd">        &gt;&gt;&gt; normal_pdf = lambda x: multivariate_normal(x, [0, 0], [[1, 0], [0, 1]])</span>
<span class="sd">        &gt;&gt;&gt; phi = ContinuousFactor([&#39;x1&#39;, &#39;x2&#39;], normal_pdf)</span>
<span class="sd">        &gt;&gt;&gt; phi.scope()</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">variables</span></div>

    <span class="k">def</span> <span class="nf">get_evidence</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">()[</span><span class="mi">1</span><span class="p">:]</span>

<div class="viewcode-block" id="ContinuousFactor.assignment"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.assignment">[docs]</a>    <span class="k">def</span> <span class="nf">assignment</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of pdf assignments for the corresponding values.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        *args: values</span>
<span class="sd">            Values whose assignment is to be computed.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        &gt;&gt;&gt; from scipy.stats import multivariate_normal</span>
<span class="sd">        &gt;&gt;&gt; normal_pdf = lambda x1, x2: multivariate_normal.pdf((x1, x2), [0, 0], [[1, 0], [0, 1]])</span>
<span class="sd">        &gt;&gt;&gt; phi = ContinuousFactor([&#39;x1&#39;, &#39;x2&#39;], normal_pdf)</span>
<span class="sd">        &gt;&gt;&gt; phi.assignment(1, 2)</span>
<span class="sd">        0.013064233284684921</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">assignment</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span></div>

<div class="viewcode-block" id="ContinuousFactor.copy"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a copy of the distribution.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ContinuousFactor object: copy of the distribution</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from scipy.special import beta</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        # Two variable dirichlet distribution with alpha = (1,2)</span>
<span class="sd">        &gt;&gt;&gt; def dirichlet_pdf(x, y):</span>
<span class="sd">        ...     return (np.power(x, 1) * np.power(y, 2)) / beta(x, y)</span>
<span class="sd">        &gt;&gt;&gt; dirichlet_factor = ContinuousFactor([&#39;x&#39;, &#39;y&#39;], dirichlet_pdf)</span>
<span class="sd">        &gt;&gt;&gt; dirichlet_factor.variables</span>
<span class="sd">        [&#39;x&#39;, &#39;y&#39;]</span>
<span class="sd">        &gt;&gt;&gt; copy_factor = dirichlet_factor.copy()</span>
<span class="sd">        &gt;&gt;&gt; copy_factor.variables</span>
<span class="sd">        [&#39;x&#39;, &#39;y&#39;]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">ContinuousFactor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span></div>

<div class="viewcode-block" id="ContinuousFactor.discretize"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.discretize">[docs]</a>    <span class="k">def</span> <span class="nf">discretize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">method</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Discretizes the continuous distribution into discrete</span>
<span class="sd">        probability masses using various methods.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        method : A Discretizer Class from pgmpy.discretize</span>

<span class="sd">        *args, **kwargs:</span>
<span class="sd">            The parameters to be given to the Discretizer Class.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        An n-D array or a DiscreteFactor object according to the discretiztion</span>
<span class="sd">        method used.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from scipy.special import beta</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import RoundingDiscretizer</span>
<span class="sd">        &gt;&gt;&gt; def dirichlet_pdf(x, y):</span>
<span class="sd">        ...     return (np.power(x, 1) * np.power(y, 2)) / beta(x, y)</span>
<span class="sd">        &gt;&gt;&gt; dirichlet_factor = ContinuousFactor([&#39;x&#39;, &#39;y&#39;], dirichlet_pdf)</span>
<span class="sd">        &gt;&gt;&gt; dirichlet_factor.discretize(RoundingDiscretizer, low=1, high=2, cardinality=5)</span>
<span class="sd">        # TODO: finish this</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span><span class="o">.</span><span class="n">get_discrete_values</span><span class="p">()</span></div>

<div class="viewcode-block" id="ContinuousFactor.reduce"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.reduce">[docs]</a>    <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reduces the factor to the context of the given variable values.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        values: list, array-like</span>
<span class="sd">            A list of tuples of the form (variable_name, variable_value).</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new ContinuosFactor object.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ContinuousFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                                  if inplace=False returns a new ContinuousFactor instance.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from scipy.special import beta</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        &gt;&gt;&gt; def custom_pdf(x, y, z):</span>
<span class="sd">        ...     return z*(np.power(x, 1) * np.power(y, 2)) / beta(x, y)</span>
<span class="sd">        &gt;&gt;&gt; custom_factor = ContinuousFactor([&#39;x&#39;, &#39;y&#39;, &#39;z&#39;], custom_pdf)</span>
<span class="sd">        &gt;&gt;&gt; custom_factor.variables</span>
<span class="sd">        [&#39;x&#39;, &#39;y&#39;, &#39;z&#39;]</span>
<span class="sd">        &gt;&gt;&gt; custom_factor.assignment(1, 2, 3)</span>
<span class="sd">        24.0</span>

<span class="sd">        &gt;&gt;&gt; custom_factor.reduce([(&#39;y&#39;, 2)])</span>
<span class="sd">        &gt;&gt;&gt; custom_factor.variables</span>
<span class="sd">        [&#39;x&#39;, &#39;z&#39;]</span>
<span class="sd">        &gt;&gt;&gt; custom_factor.assignment(1, 3)</span>
<span class="sd">        24.0</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="n">phi</span><span class="o">.</span><span class="n">distribution</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

<div class="viewcode-block" id="ContinuousFactor.marginalize"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.marginalize">[docs]</a>    <span class="k">def</span> <span class="nf">marginalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Marginalize the factor with respect to the given variables.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list, array-like</span>
<span class="sd">            List of variables with respect to which factor is to be maximized.</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new ContinuousFactor instance.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new ContinuousFactor instance.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        &gt;&gt;&gt; from scipy.stats import multivariate_normal</span>
<span class="sd">        &gt;&gt;&gt; std_normal_pdf = lambda *x: multivariate_normal.pdf(x, [0, 0], [[1, 0], [0, 1]])</span>
<span class="sd">        &gt;&gt;&gt; std_normal = ContinuousFactor([&#39;x1&#39;, &#39;x2&#39;], std_normal_pdf)</span>
<span class="sd">        &gt;&gt;&gt; std_normal.scope()</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;]</span>
<span class="sd">        &gt;&gt;&gt; std_normal.assignment([1, 1])</span>
<span class="sd">        0.058549831524319168</span>
<span class="sd">        &gt;&gt;&gt; std_normal.marginalize([&#39;x2&#39;])</span>
<span class="sd">        &gt;&gt;&gt; std_normal.scope()</span>
<span class="sd">        [&#39;x1&#39;]</span>
<span class="sd">        &gt;&gt;&gt; std_normal.assignment(1)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">distribution</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">marginalize</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

<div class="viewcode-block" id="ContinuousFactor.normalize"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.normalize">[docs]</a>    <span class="k">def</span> <span class="nf">normalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Normalizes the pdf of the continuous factor so that it integrates to</span>
<span class="sd">        1 over all the variables.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ContinuousFactor or None:</span>
<span class="sd">             if inplace=True (default) returns None</span>
<span class="sd">             if inplace=False returns a new ContinuousFactor instance.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        &gt;&gt;&gt; from scipy.stats import multivariate_normal</span>
<span class="sd">        &gt;&gt;&gt; std_normal_pdf = lambda x: 2 * multivariate_normal.pdf(x, [0, 0], [[1, 0], [0, 1]])</span>
<span class="sd">        &gt;&gt;&gt; std_normal = ContinuousFactor([&#39;x1&#39;, &#39;x2&#39;], std_normal_pdf)</span>
<span class="sd">        &gt;&gt;&gt; std_normal.assignment(1, 1)</span>
<span class="sd">        0.117099663049</span>
<span class="sd">        &gt;&gt;&gt; std_normal.normalize()</span>
<span class="sd">        &gt;&gt;&gt; std_normal.assignment(1, 1)</span>
<span class="sd">        0.0585498315243</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">distriution</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">normalize</span><span class="p">(</span><span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

    <span class="k">def</span> <span class="nf">is_valid_cpd</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">is_valid_cpd</span><span class="p">()</span>        

    <span class="k">def</span> <span class="nf">_operate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gives the ContinuousFactor operation (product or divide) with</span>
<span class="sd">        the other factor.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        other: ContinuousFactor</span>
<span class="sd">            The ContinuousFactor to be multiplied.</span>

<span class="sd">        operation: String</span>
<span class="sd">            &#39;product&#39; for multiplication operation and &#39;divide&#39; for</span>
<span class="sd">            division operation.</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ContinuousFactor or None:</span>
<span class="sd">                        if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">ContinuousFactor</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;ContinuousFactor objects can only be multiplied &quot;</span><span class="p">,</span>
                            <span class="s2">&quot;or divided with another ContinuousFactor object. &quot;</span><span class="p">,</span>
                            <span class="s2">&quot;Got </span><span class="si">{other_type}</span><span class="s2">, expected: ContinuousFactor.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                                <span class="n">other_type</span><span class="o">=</span><span class="nb">type</span><span class="p">(</span><span class="n">other</span><span class="p">)))</span>

        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">distribution</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">distribution</span><span class="o">.</span><span class="n">_operate</span><span class="p">(</span>
            <span class="n">other</span><span class="o">=</span><span class="n">other</span><span class="o">.</span><span class="n">distribution</span><span class="p">,</span> <span class="n">operation</span><span class="o">=</span><span class="n">operation</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span>

<div class="viewcode-block" id="ContinuousFactor.product"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.product">[docs]</a>    <span class="k">def</span> <span class="nf">product</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gives the ContinuousFactor product with the other factor.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        other: ContinuousFactor</span>
<span class="sd">            The ContinuousFactor to be multiplied.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ContinuousFactor or None:</span>
<span class="sd">                        if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `ContinuousFactor` instance.</span>

<span class="sd">        Example</span>
<span class="sd">        -------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        &gt;&gt;&gt; from scipy.stats import multivariate_normal</span>
<span class="sd">        &gt;&gt;&gt; sn_pdf1 = lambda x: multivariate_normal.pdf([x], [0], [[1]])</span>
<span class="sd">        &gt;&gt;&gt; sn_pdf2 = lambda x1,x2: multivariate_normal.pdf([x1, x2], [0, 0], [[1, 0], [0, 1]])</span>
<span class="sd">        &gt;&gt;&gt; sn1 = ContinuousFactor([&#39;x2&#39;], sn_pdf1)</span>
<span class="sd">        &gt;&gt;&gt; sn2 = ContinuousFactor([&#39;x1&#39;, &#39;x2&#39;], sn_pdf2)</span>

<span class="sd">        &gt;&gt;&gt; sn3 = sn1.product(sn2, inplace=False)</span>
<span class="sd">        &gt;&gt;&gt; sn3.assignment(0, 0)</span>
<span class="sd">        0.063493635934240983</span>

<span class="sd">        &gt;&gt;&gt; sn3 = sn1 * sn2</span>
<span class="sd">        &gt;&gt;&gt; sn3.assignment(0, 0)</span>
<span class="sd">        0.063493635934240983</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_operate</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="s1">&#39;product&#39;</span><span class="p">,</span> <span class="n">inplace</span><span class="p">)</span></div>

<div class="viewcode-block" id="ContinuousFactor.divide"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.ContinuousFactor.ContinuousFactor.divide">[docs]</a>    <span class="k">def</span> <span class="nf">divide</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gives the ContinuousFactor divide with the other factor.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        other: ContinuousFactor</span>
<span class="sd">            The ContinuousFactor to be multiplied.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ContinuousFactor or None:</span>
<span class="sd">                        if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        Example</span>
<span class="sd">        -------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousFactor</span>
<span class="sd">        &gt;&gt;&gt; from scipy.stats import multivariate_normal</span>
<span class="sd">        &gt;&gt;&gt; sn_pdf1 = lambda x: multivariate_normal.pdf([x], [0], [[1]])</span>
<span class="sd">        &gt;&gt;&gt; sn_pdf2 = lambda x1,x2: multivariate_normal.pdf([x1, x2], [0, 0], [[1, 0], [0, 1]])</span>
<span class="sd">        &gt;&gt;&gt; sn1 = ContinuousFactor([&#39;x2&#39;], sn_pdf1)</span>
<span class="sd">        &gt;&gt;&gt; sn2 = ContinuousFactor([&#39;x1&#39;, &#39;x2&#39;], sn_pdf2)</span>

<span class="sd">        &gt;&gt;&gt; sn4 = sn2.divide(sn1, inplace=False)</span>
<span class="sd">        &gt;&gt;&gt; sn4.assignment(0, 0)</span>
<span class="sd">        0.3989422804014327</span>

<span class="sd">        &gt;&gt;&gt; sn4 = sn2 / sn1</span>
<span class="sd">        &gt;&gt;&gt; sn4.assignment(0, 0)</span>
<span class="sd">        0.3989422804014327</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">()):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Scope of divisor should be a subset of dividend&quot;</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_operate</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="s1">&#39;divide&#39;</span><span class="p">,</span> <span class="n">inplace</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">__mul__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__rmul__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__mul__</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__truediv__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">divide</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="n">__div__</span> <span class="o">=</span> <span class="n">__truediv__</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>